package com.mogist.hqc.view;

import android.support.design.widget.TabLayout;
import android.view.View;

import java.lang.reflect.Field;

/**
 * 作用：TabLayout 添加点击事件的帮助类
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
public class TabLayoutAddOnClickHelper
{
	public static void AddOnClick(TabLayout tabLayout, View.OnTouchListener listener)
	{
		for(int i = 0; i < tabLayout.getTabCount(); i++)
		{
			View view = getTabView(tabLayout, i);
			if(view == null)
			{
				continue;
			}
			view.setTag(i);
			view.setOnTouchListener(listener);
		}
	}

	// 获取tabview
	private static View getTabView(TabLayout tabLayout, int index)
	{
		TabLayout.Tab tab = tabLayout.getTabAt(index);
		if(tab == null)
		{
			return null;
		}
		View tabView = null;
		Field view = null;
		try
		{
			view = TabLayout.Tab.class.getDeclaredField("view");
		}
		catch(NoSuchFieldException e)
		{
			e.printStackTrace();
		}
		view.setAccessible(true);
		try
		{
			tabView = (View) view.get(tab);
		}
		catch(IllegalAccessException e)
		{
			e.printStackTrace();
		}
		return tabView;
	}
}
